using System;

namespace DocLibApp
{
	/// <summary>
	/// Exception Handler
	/// general class for handling exception (write log, show detail message,etc)
	/// </summary>
	public class eh
	{
		/// <summary>
		/// Write exception to a log file?
		/// </summary>
		private bool bWriteLog = true;
		private string sLineSep = "======================================================================================";

		/// <summary>
		/// Exception log file name
		/// </summary>
		private string sExceptionLogFileName = @"ExceptionLog.txt";

		#region Constructor
		/// <summary>
		/// Exception Handler,default Exception log file "ExceptionLog.txt"
		/// </summary>
		public eh()
		{
			this.sExceptionLogFileName = @"ExceptionLog.txt";
		}

		/// <summary>
		/// Exception Handler, indicate if write log should be enabled
		/// </summary>
		/// <param name="bWriteLog"></param>
		public eh(bool bWriteLog)
		{
            this.bWriteLog = bWriteLog;
		}

        /// <summary>
        /// Exception Handler, specify a Exception Log file name
        /// </summary>
        /// <param name="sFileName"></param>
		public eh(string sFileName)
		{
			this.sExceptionLogFileName = sFileName;
		}
		#endregion // Constructor

		#region Public functions
		/// <summary>
		/// Write detail exception message to log file
		/// </summary>
		/// <param name="ex">Exception</param>
		public void WriteExpLog(System.Exception ex)
		{
            string res = System.DateTime.Now.ToString() + ":";
            res +=  System.Environment.NewLine + "MSG = " + ex.Message;

            if (ex.Source.Trim() != "")
                res += System.Environment.NewLine + "SRC = " + ex.Source;

            if (ex.StackTrace.Trim() != "")
                res += System.Environment.NewLine + "STK = " + ex.StackTrace;

            res += System.Environment.NewLine + sLineSep;

            WriteLogFile(sExceptionLogFileName, res);
		}

		/// <summary>
		/// Write detail exception log message including source
		/// </summary>
		/// <param name="source"></param>
		/// <param name="ex"></param>
		public void WriteExpLog(string source, System.Exception ex)
		{
			if (this.bWriteLog)
			{
				WriteLogFile(sExceptionLogFileName, System.DateTime.Now.ToString() + " : (from " + source + ")" );
				WriteLogFile(sExceptionLogFileName, "MSG = " + ex.Message);

				if (ex.StackTrace.Trim() != "")
					WriteLogFile(sExceptionLogFileName, "STK = " + ex.StackTrace);
				if (ex.Source.Trim() != "")
					WriteLogFile(sExceptionLogFileName, "SRC = " + ex.Source);

				WriteLogFile(sExceptionLogFileName, sLineSep);
			}
		}

		/// <summary>
		/// Displays detail exception message using windows form
		/// </summary>
		/// <param name="ex">Exception</param>
		public void ShowExp(System.Exception ex)
		{
			ehShow es = new ehShow(ex);
		}
		#endregion //Public functions

        /// <summary>
        /// Writes a log entry to a log file
        /// </summary>
        /// <param name="sLogFileName">log file name</param>
        /// <param name="sMessageLine">Log Entry line</param>
        private void WriteLogFile(string sLogFileName, string sMessageLine)
        {
            System.IO.FileStream fs = new System.IO.FileStream(sLogFileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            System.IO.StreamWriter m_streamWriter = new System.IO.StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, System.IO.SeekOrigin.End);
            m_streamWriter.WriteLine(sMessageLine);
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }
	}
}
